/*
 *所有关于Recordsinspections类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*RecordsinspectionsService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyMES.IRepositories;
using MyMes.Infrastructure;
using MyMES.Dto;
using MyMES.Infrastructure;
using MyMES.Repositories;

namespace MyMES.Services
{
    public partial class RecordsinspectionsService
    {
        private readonly IBaseRepository<Recordsinspections> _recordsinspectionsRepository;

        public RecordsinspectionsService(IBaseRepository<Recordsinspections> recordsinspectionsRepository)
        {
            _recordsinspectionsRepository = recordsinspectionsRepository;
        }

        /// <summary>
        /// 获取点检记录
        /// </summary>
        /// <returns></returns>
        public async Task<APIPage<Recordsinspections>> GetRecordsinspectionsList(GetRecordsinspectionsListDTO request)
        {
            var list = _recordsinspectionsRepository.GetAll();

            if (!string.IsNullOrEmpty(request.EquipmentCode))
            {
                list = list.Where(e => e.EquipmentCode.Contains(request.EquipmentCode));
            }

            if (!string.IsNullOrEmpty(request.EquipmentName))
            {
                list = list.Where(e => e.EquipmentName.Contains(request.EquipmentName));
            }

            if (!string.IsNullOrEmpty(request.PlanName))
            {
                list = list.Where(e => e.PlanName.Contains(request.PlanName));
            }

            var supplierpaging = new APIPage<Recordsinspections>()
            {
                Data = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList(),
                TotalCount = list.Count(),
                TotalPage = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize)
            };

            return supplierpaging;
        }

        /// <summary>
        /// 新增点检记录
        /// </summary>
        /// <param name="param">点检记录参数</param>
        /// <returns></returns>
        public async Task<int> CreateRecordsinspections(Recordsinspections param)
        {
            var result = await _recordsinspectionsRepository.AddAsync(param);

            return result;
        }

        /// <summary>
        /// 删除点检记录
        /// </summary>
        /// <param name="param">点检记录参数</param>
        /// <returns></returns>
        public async Task<int> DeleteRecordsinspections(Recordsinspections param)
        {
            var result = await _recordsinspectionsRepository.Remove(param);

            return result;
        }

        /// <summary>
        /// 修改点检记录
        /// </summary>
        /// <param name="param">点检记录参数</param>
        /// <returns></returns>
        public async Task<int> UpdateRecordsinspections(Recordsinspections param)
        {
            var result = await _recordsinspectionsRepository.UpdateAsync(param);

            return result;
        }
    }
}
